import numpy as np
import matplotlib.pyplot as plt
#支持中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
# 支持负数
plt.rcParams['axes.unicode_minus'] = False
# 生成数据(正弦函数)
x = np.linspace(-2*np.pi, 2*np.pi, 1000)
y = np.sin(x)
# 创建图形和坐标轴
fig, ax = plt.subplots(figsize=(10, 6))
# 绘制正弦曲线
ax.plot(x, y, color='#FF4B4B', linewidth=2, label='y = sin(x)')
# 设置坐标原点到(0,0)
ax.spines['left'].set_position('center')    # X轴线移动到y=0
ax.spines['bottom'].set_position('center')  # Y轴线移动到x=0
ax.spines['right'].set_color('none')        # 隐藏右侧轴线
ax.spines['top'].set_color('none')          # 隐藏顶部轴线
# 设置坐标轴刻度
ax.set_xticks([
    -2*np.pi, -3*np.pi/2, -np.pi, -np.pi/2, 0,
    np.pi/2, np.pi, 3*np.pi/2, 2*np.pi
])
ax.set_yticks([-1, -0.5, 0, 0.5, 1])
# 设置π符号刻度标签
ax.set_xticklabels([
    r'$-2\pi$', r'$-\frac{3\pi}{2}$', r'$-\pi$', 
    r'$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$',
    r'$\pi$', r'$\frac{3\pi}{2}$', r'$2\pi$'
], fontsize=10)
# 设置坐标范围
ax.set_xlim(-2*np.pi-0.1, 2*np.pi+0.1)
ax.set_ylim(-1.2, 1.2)
# 添加图例和标题
ax.legend(loc='upper right')
ax.set_title('正弦函数坐标系', pad=20, fontsize=14, fontweight='bold')
# 显示图形
plt.show()